package com.appcenter.task.controller;

import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.config.JsonResult;
import com.appcenter.task.input.SearchAlreadyTaskListInput;
import com.appcenter.task.input.SearchUnTaskListInput;
import com.appcenter.task.input.externalapi.*;
import com.appcenter.task.service.ExternalAPISercive;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("services/api")
@Api(value="开发API",description = "开发API")
public class ExternalAPIController {

    @Autowired
    ExternalAPISercive externalAPIService;

    @ApiOperation(value = "推送待办", notes = "推送待办",produces = "application/json")
    @PostMapping("/sendtask")
    public ResponseEntity<JsonResult> SendTask(@RequestBody SendTaskInput input) throws Exception {
        JsonResult r = new JsonResult();
        try {
            r.setResult(externalAPIService.SendTask(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.ok(r);
        } catch(Exception e){
            ErrorJsonResult curErrorJsonResult=new ErrorJsonResult("100",e.getMessage().toString());
            r.setError(curErrorJsonResult);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "待办转已办", notes = "待办转已办",produces = "application/json")
    @PostMapping("/conversiontask")
    public ResponseEntity<JsonResult> ConversionTask(@RequestBody ConversionTaskInput input) throws Exception {
        JsonResult r = new JsonResult();
        try {
            r.setResult(externalAPIService.ConversionTask(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.ok(r);
        } catch(Exception e){
            ErrorJsonResult curErrorJsonResult=new ErrorJsonResult("100",e.getMessage().toString());
            r.setError(curErrorJsonResult);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "获取待办列表数据", notes = "获取待办列表数据",produces = "application/json")
    @PostMapping("/getuntasks")
    public ResponseEntity<JsonResult> GetUnTasks(@RequestBody SearchUnTaskListInput input) throws Exception {
        JsonResult r = new JsonResult();
        try {
            r.setResult(externalAPIService.GetUnTasks(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.ok(r);
        } catch(Exception e){
            ErrorJsonResult curErrorJsonResult=new ErrorJsonResult("100",e.getMessage().toString());
            r.setError(curErrorJsonResult);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "获取单条待办信息", notes = "获取单条待办信息",produces = "application/json")
    @PostMapping("/getuntask")
    public ResponseEntity<JsonResult> GetUnTask(@RequestBody GetUnTaskInput input) throws Exception {
        JsonResult r = new JsonResult();
        try {
            r.setResult(externalAPIService.GetUnTask(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.ok(r);
        } catch(Exception e){
            ErrorJsonResult curErrorJsonResult=new ErrorJsonResult("100",e.getMessage().toString());
            r.setError(curErrorJsonResult);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }


    @ApiOperation(value = "获取已办列表数据", notes = "获取已办列表数据",produces = "application/json")
    @PostMapping("/getalreadytasks")
    public ResponseEntity<JsonResult> GetAlreadys(@RequestBody SearchAlreadyTaskListInput input) throws Exception {
        JsonResult r = new JsonResult();
        try {
            r.setResult(externalAPIService.GetAlreadys(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.ok(r);
        } catch(Exception e){
            ErrorJsonResult curErrorJsonResult=new ErrorJsonResult("100",e.getMessage().toString());
            r.setError(curErrorJsonResult);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

    @ApiOperation(value = "获取单条已办数据", notes = "获取单条已办数据",produces = "application/json")
    @PostMapping("/getalreadytask")
    public ResponseEntity<JsonResult> GetAlready(@RequestBody GetAlreadyInput input) throws Exception {
        JsonResult r = new JsonResult();
        try {
            r.setResult(externalAPIService.GetAlready(input));
        } catch (ErrorJsonResult e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setError(e);
            e.printStackTrace();
            return ResponseEntity.ok(r);
        } catch(Exception e){
            ErrorJsonResult curErrorJsonResult=new ErrorJsonResult("100",e.getMessage().toString());
            r.setError(curErrorJsonResult);
            e.printStackTrace();
            return ResponseEntity.status(500).body(r);
        }
        return ResponseEntity.ok(r);
    }

}
